package com.neudu.glsb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neudu.glsb.commons.JSONReturn;
import com.neudu.glsb.pojo.Employees;
import com.neudu.glsb.pojo.Schedules;
import com.neudu.glsb.service.EmployeesService;
import com.neudu.glsb.service.SchedulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class ScheduleController {

    @Autowired
    private JSONReturn jsonReturn;

    @Autowired
    private SchedulesService schedulesService;

    @Autowired
    private EmployeesService employeesService;

    // 获取所有排班信息，支持按姓名模糊查询
    @PostMapping("/getAllSchedules")
    public String getAllSchedules(@RequestBody Map<String, Object> pageInfoMap) {
        try {
            Integer pageNum = (Integer) pageInfoMap.get("pageNum");
            Integer pageSize = (Integer) pageInfoMap.get("pageSize");
            String employeeName = (String) pageInfoMap.get("employeeName"); // 获取姓名查询参数

            // 查询员工信息
            List<Employees> employeesList;
            if (!ObjectUtils.isEmpty(employeeName)) {
                employeesList = employeesService.list(new LambdaQueryWrapper<Employees>().like(Employees::getEmployeeName, employeeName));
            } else {
                employeesList = employeesService.list();
            }

            // 获取员工ID列表
            List<Integer> employeeIds = employeesList.stream().map(Employees::getId).collect(Collectors.toList());

            // 查询排班信息
            Page<Schedules> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<Schedules> queryWrapper = new LambdaQueryWrapper<>();
            if (!employeeIds.isEmpty()) {
                queryWrapper.in(Schedules::getEmployeeId, employeeIds);
            }
            Page<Schedules> schedulePage = schedulesService.page(page, queryWrapper);

            // 将员工姓名加入排班信息
            List<Schedules> records = schedulePage.getRecords();
            for (Schedules schedule : records) {
                Employees employee = employeesList.stream()
                        .filter(e -> e.getId().equals(schedule.getEmployeeId()))
                        .findFirst()
                        .orElse(null);
                if (employee != null) {
                    schedule.setEmployeeName(employee.getEmployeeName()); // 动态添加员工姓名
                }
            }

            return jsonReturn.returnSuccess(schedulePage);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据排班 ID 获取排班信息
    @PostMapping("/getScheduleById")
    public String getScheduleById(@RequestBody Map<String, Integer> idMap) {
        try {
            Integer id = idMap.get("id");
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("排班ID不能为空");
            }
            Schedules schedule = schedulesService.getById(id);
            if (schedule != null) {
                Employees employee = employeesService.getById(schedule.getEmployeeId());
                if (employee != null) {
                    schedule.setEmployeeName(employee.getEmployeeName()); // 动态添加员工姓名
                }
            }
            return jsonReturn.returnSuccess(schedule);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 新增排班信息
    @PostMapping("/addSchedule")
    public String addSchedule(@RequestBody Schedules schedule) {
        try {
            // 检查员工是否存在
            Employees employee = employeesService.getById(schedule.getEmployeeId());
            if (employee == null) {
                return jsonReturn.returnError("员工ID不存在");
            }
            schedule.setEmployeeName(employee.getEmployeeName()); // 设置员工姓名
            boolean success = schedulesService.save(schedule);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("新增排班信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 修改排班信息
    @PostMapping("/updateSchedule")
    public String updateSchedule(@RequestBody Schedules schedule) {
        try {
            boolean success = schedulesService.updateById(schedule);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("修改排班信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 删除排班信息
    @PostMapping("/deleteSchedule")
    public String deleteSchedule(@RequestBody Map<String, Integer> idMap) {
        try {
            Integer id = idMap.get("id");
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("排班ID不能为空");
            }
            boolean success = schedulesService.removeById(id);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("删除排班信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}